# coding: utf-8
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
from common.overwrites import z_render_to_response
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required

from django.conf import settings
from common.models.fun import Bottle
from common.services.logic import LogicService
from common.models.fun import Bottle, BottleGetTrace, BottleReplyTrace
from django.views.decorators.cache import cache_page
import time


@login_required
@csrf_exempt
def index(request):
    bottles_which_i_send = Bottle.objects.filter(sender=request.user, is_del=False).order_by('-id')


    bottle_trace_ids = BottleGetTrace.objects.filter(getter=request.user, is_del=False).order_by('-id').values_list('bottle', flat=True)
    bottles_which_i_got = Bottle.objects.filter(id__in=bottle_trace_ids)

    return z_render_to_response('bottle.html', {'bottles_which_i_send': bottles_which_i_send,
                                                'bottles_which_i_got': bottles_which_i_got})



@login_required
@csrf_exempt
def bottle_send(request):
    if request.method == 'POST':
        content = request.POST.get('content')

        Bottle.objects.create(sender=request.user, content=content)
        return JsonResponse({'success': True})


@login_required
@csrf_exempt
def bottle_get(request):
    if request.method == 'GET':
        want_type = request.GET.get('want_type')

        bottle = LogicService.get_a_bottle_on_the_ocean(request.user, want_type)
        print '----------b', bottle
        if bottle:
            result = {'success': True, 'bottle_id': bottle.id}
        else:
            result = {'success': False, 'bottle_id': -1}

        return JsonResponse(result)


@login_required
@csrf_exempt
def bottle_detail(request):
    # if request.method == 'GET':
    # print time.time()
    bottle = Bottle.objects.get(id=int(request.GET.get('bottle_id')))
    replys = BottleReplyTrace.objects.filter(bottle=bottle)
    # print time.time()
    return z_render_to_response('bottle_detail.html', {'bottle': bottle, 'replys': replys})


@login_required
@csrf_exempt
def bottle_reply(request):
    if request.method == 'POST':
        content = request.POST.get('content')
        bottle = Bottle.objects.get(id=int(request.POST.get('bottle_id')))

        if hasattr(bottle, 'bottlegettrace'):
            assert request.user == bottle.bottlegettrace.getter
        else:
            assert request.user == bottle.sender

        BottleReplyTrace.objects.create(user=request.user, content=content, bottle=bottle)
        return JsonResponse({'success': True})


@login_required
@csrf_exempt
def bottle_del(request):
    if request.method == 'POST':
        bottle = Bottle.objects.get(id=int(request.POST.get('bottle_id')))

        if request.user == bottle.sender:
            bottle.is_del = True
            bottle.save()
            return JsonResponse({'success': True})

        if request.user == bottle.bottlegettrace.getter:
            trace = BottleGetTrace.objects.get(id=bottle.bottlegettrace.id)
            trace.is_del = True
            trace.save()
            return JsonResponse({'success': True})

